
package tomkitty.server;

import tomkitty.turdlet.*;
import tomkitty.util.*;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 *
 * Multithreaded HTTP server class.
 *
 * @author alan, micha
 */
public class Server {

	private ServerSocket s;

	public Server(int port, int minthreads, int maxthreads) {

		ServerSocket s = null;

		ConnectionHandlerPool pool = 
			new ConnectionHandlerPool(minthreads, maxthreads);

		try {
			s = new ServerSocket(port);
		} catch(IOException e) {
			System.out.println(e);
			System.exit(1);
		}

		while(true) {
			Socket in = null;

			try {
				in = s.accept();
			} catch(IOException e) {
				System.out.println(e);
				continue;
			}

			ConnectionHandler th = pool.getConnectionHandler();
			if (th != null) {
				th.setSocket(in);
			} else {
				// too many threads
				// maybe want to shit out some error codes here?
				try {
					in.close();
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

